﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Yipusoft.Common.BLL;
using Yipusoft.Exercitation.BLL;
using Yipusoft.Exercitation.Model;
using System.Drawing;

public partial class Combat_NewExam2 : System.Web.UI.Page
{
    string id = "55F5139F-489A-487D-8D55-FF6E555C7FDA";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Bind();
            TaskBaseInfo baseInfo= TaskBase.GetTaskBase(new Guid(Request["ID"]));
            if(baseInfo!=null)
            {
                lblTaskTitle.Text = baseInfo.TaskTitle + " 【"+baseInfo.StartDate.ToString("yyyy年MM月dd日")+"--"+baseInfo.EndDate.ToString("yyyy年MM月dd日】");
            }
        }
       
    }

    public void chkAll_CheckedChanged(object sender, EventArgs e)
    {
        CheckBox chkbox = sender as CheckBox;
        CheckBoxList chkList= chkbox.Parent.FindControl("chkSkill") as CheckBoxList;
        foreach (ListItem item in chkList.Items)
        {
            item.Selected = chkbox.Checked;
        }
    }
    private void Bind()
    {
        ddlRank.DataSource = CodeItem.GetList(" Left(ItemID,6)='ZXSBJX' Order by ItemID");
        ddlRank.DataTextField = "ItemName";
        ddlRank.DataValueField = "ItemID";
        ddlRank.DataBind();

        ddlStation.DataSource = CodeItem.GetList(" Left(ItemID,4)='SBGW' Order by ItemID");
        ddlStation.DataTextField = "ItemName";
        ddlStation.DataValueField = "ItemID";
        ddlStation.DataBind();
        BindList();

        chkDepartment.DataSource = Yipusoft.Account.BLL.Department.GetList(" DepartmentID IN(Select DepartmentID From TaskBrigade Where TaskID='" + Request["ID"] + "')");
        chkDepartment.DataTextField = "FullName";
        chkDepartment.DataValueField = "DepartmentID";
        chkDepartment.DataBind();
        chkDepartment.RepeatColumns = 4;
        foreach (ListItem item in chkDepartment.Items)
        {
            item.Selected = true;
        }
        chkDepartment.Enabled = false;

        if (ddlRank.SelectedIndex == 0 || ddlStation.SelectedIndex == 0)
            btnSave.Visible = false;
        else
            btnSave.Visible = true;
    }

    private void BindList()
    {
        dltList.DataSource = ItemSkill.GetList(" ParentID='" + id + "'");
        dltList.DataBind();
    }
    protected void dltItemList_ItemDataBound(object sender, DataListItemEventArgs e)
    {
        Label ID = e.Item.FindControl("ID") as Label;
        if (ID == null || string.IsNullOrEmpty(ID.Text.Trim()))
            return;
        CheckBoxList chk = e.Item.FindControl("chkSkill") as CheckBoxList;
        chk.DataSource = TaskSkill.GetList(" SkillID IN (Select SkillID From ItemSkill Where ParentID='" + ID.Text + "') ");// ItemSkill.GetList(" ParentID='" + info.ID + "'");
        chk.DataTextField = "SkillName";
        chk.DataValueField = "SkillID";
        chk.DataBind();

        foreach (ListItem item in chk.Items)
        {
            List<TaskSubjectInfo> sublist = TaskSubject.GetList(" TaskItemID IN(Select ID From TaskItems Where RankID='" + ddlRank.SelectedValue + "' And StationID='" + ddlStation.SelectedValue + "' And TaskID='" + Request["ID"] + "') And SkillID='" + item.Value + "' And ItemSkillID='" + ID.Text + "'");
            if (sublist != null && sublist.Count > 0)
            {
                item.Selected = true;
            }
        }
    }
    protected void dltList_ItemDataBound(object sender, DataListItemEventArgs e)
    {
        Label ID = e.Item.FindControl("ID") as Label;
        if (ID == null || string.IsNullOrEmpty(ID.Text.Trim()))
            return;

        List<ItemSkillInfo> itemList = ItemSkill.GetList("ParentID='" + ID.Text.Trim() + "'  And ID IN (Select ItemSkillID From BaseSkill Where RankID='" + ddlRank.SelectedValue + "' And StationID='" + ddlStation.SelectedValue + "')");
        DataList dlt = e.Item.FindControl("dltItemList") as DataList;
        dlt.DataSource = itemList;
        dlt.DataBind();

    }
    protected void ddlRank_SelectedIndexChanged(object sender, EventArgs e)
    {
       
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        try
        {            
            TaskItemsInfo taskitem = new TaskItemsInfo();
            if (!string.IsNullOrEmpty(Request["Modify"]))
            {
                TaskItemsInfo info = TaskItems.GetList(" RankID='" + ddlRank.SelectedValue + "' And StationID='" + ddlStation.SelectedValue + "' And TaskID='" + Request["ID"] + "'").FirstOrDefault();
                if (info != null)
                    taskitem.ID = info.ID;
                else
                    taskitem.ID = Guid.NewGuid();
            }
            else
            {
                taskitem.ID = Guid.NewGuid();
            }

            taskitem.TaskID = new Guid(Request["ID"]);
            taskitem.RankID = ddlRank.SelectedValue;
            taskitem.StationID = ddlStation.SelectedValue;
            taskitem.StartDate = DateTime.Now;
            taskitem.EndDate = DateTime.Now;

            foreach (DataListItem item in dltList.Items)
            {
                DataList dlt = item.FindControl("dltItemList") as DataList;
                foreach (DataListItem im in dlt.Items)
                {
                    Label ID = im.FindControl("ID") as Label;
                    CheckBoxList chkList = im.FindControl("chkSkill") as CheckBoxList;
                    foreach (ListItem chk in chkList.Items)
                    {
                        if (string.IsNullOrEmpty(Request["Modify"])) //如果是添加
                        {
                            if (chk.Selected)
                            {
                                TaskSubjectInfo subinfo = new TaskSubjectInfo();
                                subinfo.TaskItemID = taskitem.ID;
                                subinfo.TaskID = new Guid(Request["ID"]);
                                subinfo.SkillID = new Guid(chk.Value.ToString());
                                subinfo.ItemSkillID = new Guid(ID.Text);
                                TaskSubject.Insert(subinfo);
                            }
                        }
                        else //如果是修改
                        {
                            if (!chk.Selected) //如果没有选中则从数据库中删除此项目
                            {
                                TaskSubject.Delete(" TaskID='" + Request["ID"] + "' And TaskItemID='" + taskitem.ID + "'  And SkillID='" + chk.Value + "' And ItemSkillID='" + ID.Text + "'");
                            }
                            else //如果是修改增加训练项目
                            {
                                List<TaskSubjectInfo> subtemp = TaskSubject.GetList(" TaskID='" + Request["ID"] + "' And TaskItemID='" + taskitem.ID + "' And SkillID='" + chk.Value + "' And ItemSkillID='" + ID.Text + "'");
                                if (subtemp == null || subtemp.Count <= 0)
                                {
                                    TaskSubjectInfo subinfo = new TaskSubjectInfo();
                                    subinfo.TaskItemID = taskitem.ID;
                                    subinfo.TaskID = new Guid(Request["ID"]);
                                    subinfo.SkillID = new Guid(chk.Value.ToString());
                                    subinfo.ItemSkillID = new Guid(ID.Text);
                                    TaskSubject.Insert(subinfo);
                                }
                            }
                        }
                    }
                }
            }
            if (!TaskItems.Exsits(taskitem.ID))
            {
                TaskItems.Insert(taskitem);
            }
            else
            {
                List<TaskSubjectInfo> stemp = TaskSubject.GetList(" TaskItemID ='" + taskitem.ID + "' And TaskID='" + Request["ID"] + "'");
                if (stemp == null || stemp.Count <= 0)
                {
                    TaskItems.Delete(taskitem.ID);
                }
            }
            Response.Write("<script>alert('保存成功！');;history.back(0);</script>");
        }
        catch
        {
            Response.Write("<script>alert('保存失败！');;history.back(0);</script>");
        }

    }
    protected void btnHome_Click(object sender, EventArgs e)
    {
        Response.Redirect("TaskList.aspx");
    }
    protected void dltItemList_ItemCommand(object source, DataListCommandEventArgs e)
    {
        
    }
    protected void ddlStation_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (ddlRank.SelectedIndex == 0 || ddlStation.SelectedIndex == 0)
            btnSave.Visible = false;
        else
            btnSave.Visible = true;
        BindList();
    }
}
